/*
 * json_tcp_parse.h
 *
 *  Created on: May 7, 2016
 *      Author: Alan Wang
 */

#ifndef BUSINESS_TCP_JSON_HELPER_H_
#define BUSINESS_TCP_JSON_HELPER_H_
#include "../../lib/cjson/cJSON.h"
#include "../../app/gvar.h"

#define MAX_WIFI_LIST_LEN 20


//response error info define
#define ERR_MSG_PERMISSION_DENIED							"permission denied!"

#define ERR_CODE_SENDMASTERINFO_OF_COMMON_FAILED			10101
#define ERR_MSG_SENDMASTERINFO_OF_COMMON_FAILED				"reveived sendMasterInfo failed!"

#define ERR_CODE_SENDWIFIINFO_OF_COMMON_FAILED				10201
#define ERR_MSG_SENDWIFIINFO_OF_COMMON_FAILED				"reveived sendWifiInfo failed!"
#define ERR_CODE_SENDWIFIINFO_OF_PERMISSION_DENIED			10202
#define ERR_MSG_SENDWIFIINFO_OF_PERMISSION_DENIED			ERR_MSG_PERMISSION_DENIED

#define ERR_CODE_MODIFYDEVICEWIFI_OF_COMMON_FAILED			10301
#define ERR_MSG_MODIFYDEVICEWIFI_OF_COMMON_FAILED			"reveived modifyDeviceWifi failed!"
#define ERR_CODE_MODIFYDEVICEWIFI_OF_PERMISSION_DENIED		10302
#define ERR_MSG_MODIFYDEVICEWIFI_OF_PERMISSION_DENIED		ERR_MSG_PERMISSION_DENIED

#define ERR_CODE_GETDEVICEUSERLIST_OF_COMMON_FAILED			10401
#define ERR_MSG_GETDEVICEUSERLIST_OF_COMMON_FAILED			"reveived getDeviceUserList failed!"
#define ERR_CODE_GETDEVICEUSERLIST_OF_PERMISSION_DENIED		10402
#define ERR_MSG_GETDEVICEUSERLIST_OF_PERMISSION_DENIED		ERR_MSG_PERMISSION_DENIED

#define ERR_CODE_DELETEDEVICEUSER_OF_COMMON_FAILED			10501
#define ERR_MSG_DELETEDEVICEUSER_OF_COMMON_FAILED			"reveived deleteDeviceUser failed!"
#define ERR_CODE_DELETEDEVICEUSER_OF_PERMISSION_DENIED		10502
#define ERR_MSG_DELETEDEVICEUSER_OF_PERMISSION_DENIED		ERR_MSG_PERMISSION_DENIED

#define ERR_CODE_UNBINDDEVICE_OF_COMMON_FAILED				10601
#define ERR_MSG_UNBINDDEVICE_OF_COMMON_FAILED				"reveived unbindDevice failed!"
#define ERR_CODE_UNBINDDEVICE_OF_PERMISSION_DENIED			10602
#define ERR_MSG_UNBINDDEVICE_OF_PERMISSION_DENIED			ERR_MSG_PERMISSION_DENIED

#define ERR_CODE_GETDEVICESTATUS_OF_COMMON_FAILED				10701
#define ERR_MSG_GETDEVICESTATUS_OF_COMMON_FAILED				"reveived getDeviceStatus failed!"

/**
 * tcp request base model
 */
typedef struct {
	int id;
	char method[20];
	char jsonrpc[5];
} MReqBase;

/**
 * tcp request of sendMaseterInfo
 */
typedef struct {
	int net_speed;
	int gpm;
	int user_count;
} DeviceState;

/**
 * tcp request
 */
typedef struct {
	MReqBase req_base;
	cJSON *req_param;
}TcpRequest;


typedef struct {
	MReqBase req_base;
	int dev_master_id;
}ReqSendMasterInfo;

typedef struct {
	MReqBase req_base;
	int dev_master_id;
	int list_len;
	WifiInfo wifi_list[MAX_WIFI_LIST_LEN];
}ReqSendWifiInfo;

typedef struct {
	MReqBase req_base;
	int dev_master_id;
	char ssid[MAX_SSID_LEN];
	char password[MAX_PWD_LEN];
}ReqModifyDeviceWifi;

typedef struct {
	MReqBase req_base;
	int dev_master_id;
}ReqGetDeviceUserList;

/**
 * DeviceUserInfo
 */
typedef struct {
	char name[MAX_SSID_LEN];
	char mac[MAX_MAC_LEN];
}DeviceUserInfo;

typedef struct {
	MReqBase req_base;
	int dev_master_id;
	char user_mac[MAX_MAC_LEN];
}ReqDeleteDeviceUser;

typedef struct {
	MReqBase req_base;
	int dev_master_id;
	char dev_mac[MAX_MAC_LEN];
}ReqUnbindDevice;

typedef struct {
	MReqBase req_base;
}ReqGetDeviceStatus;


typedef enum {
	NET_NORMAL = 1,
	NET_NOFLOWS,
	NET_WIFIERROR,
	NET_OTHERERROR
}NetStatus;

typedef enum {
	WORK_NORMAL = 1,
	WORK_NOBOUND,
	WORK_OTHERERROR
}WorkStatus;

typedef struct{
	WorkStatus work_status;
	NetStatus net_status;
}DeviceStatus;




int parse_req_to_get_cmd_id(char *json_req);

int parse_req_for_deviceMasterId(ReqSendMasterInfo *req_sendMasterInfo, char *json_req);

int parse_req_for_sendWifiInfo(ReqSendWifiInfo *req_sendWifiInfo, char *json_req);

int parse_req_for_modifyDeviceWifi(ReqModifyDeviceWifi *req_modifyDeviceWifi, char *json_req);

int parse_req_for_getDeviceUserList(ReqGetDeviceUserList *req_getDeviceUserList, char *json_req);

int parse_req_for_deleteDeviceUser(ReqDeleteDeviceUser *req_deleteDeviceUser, char *json_req);

int parse_req_for_unbindDevice(ReqUnbindDevice *req_unbindDevice, char *json_req);

int parse_req_for_getDeviceStatus(ReqGetDeviceStatus *req_getDeviceStatus, char *json_req);



char *get_json_str_of_rep_sendMasterInfo(int status);
char *get_json_str_of_error_sendMasterInfo(int err_code, char *debug_msg);

char *get_json_str_of_rep_sendWifiInfo(int status);
char *get_json_str_of_error_sendWifiInfo(int err_code, char *debug_msg);

char *get_json_str_of_rep_modifyDeviceWifi(int status);
char *get_json_str_of_error_modifyDeviceWifi(int err_code, char *debug_msg);

char *get_json_str_of_rep_getDeviceUserList(DeviceUserInfo *dev_usr_list, int count);
char *get_json_str_of_error_getDeviceUserList(int err_code, char *debug_msg);

char *get_json_str_of_rep_deleteDeviceUser(int status);
char *get_json_str_of_error_deleteDeviceUser(int err_code, char *debug_msg);

char *get_json_str_of_rep_unbindDevice(int status);
char *get_json_str_of_error_unbindDevice(int err_code, char *debug_msg);

char *get_json_str_of_rep_getDeviceStatus(DeviceStatus dev_status);
char *get_json_str_of_error_getDeviceStatus(int err_code, char *debug_msg);




char *get_json_str_of_notify_device_state(DeviceState device_state);
char *get_json_str_of_notify_net_status(NetStatus net_status);

int get_app_res_status_by_json(char *res_status_json);


#endif /* BUSINESS_TCP_JSON_HELPER_H_ */
